package com.yhy;

import com.alibaba.fastjson.JSON;
import com.yhy.wxzn.common.utils.DateUtil;
import com.yhy.wxzn.common.utils.SpringUtil;
import com.yhy.wxzn.common.utils.WcsUtils;
import com.yhy.wxzn.wcs.device.plc.task.PlcTaskThread;
import com.yhy.wxzn.wcs.device.rgv.bean.Coord;
import com.yhy.wxzn.wcs.device.rgv.bean.Node;
import com.yhy.wxzn.wcs.device.rgv.bean.Route;
import com.yhy.wxzn.wcs.device.rgv.core.avoid.AvoidHandler;
import com.yhy.wxzn.wcs.device.rgv.core.pathplanning.PathPlaningHandlerImpl;
import com.yhy.wxzn.wcs.device.rgv.enums.RgvProgress;
import com.yhy.wxzn.wcs.entity.WcsFourWayTask;
import com.yhy.wxzn.wcs.enums.TaskStatus;
import com.yhy.wxzn.wcs.service.impl.WcsDeviceInfoServiceImpl;
import com.yhy.wxzn.wcs.service.impl.WcsFourWayTaskServiceImpl;
import com.yhy.wxzn.wcs.service.impl.WcsMapInfoServiceImpl;
import com.yhy.wxzn.wcs.service.impl.WcsTaskServiceImpl;
import com.yhy.wxzn.wcs.wcsWmsCommunication.ToWmsImpl;
import com.yhy.wxzn.wcs.wcsWmsCommunication.bean.TaskApply;
import com.yhy.wxzn.wcs.wcsWmsCommunication.bean.TaskStatusRtn;
import com.yhy.wxzn.wcs.wcsWmsCommunication.bean.WmsResponses;
import com.yhy.wxzn.wcs.websocket.bean.FloorInfo;
import com.yhy.wxzn.wcs.websocket.bean.LiferInfo;
import com.yhy.wxzn.wcs.websocket.bean.NodeInfo;
import com.yhy.wxzn.wcs.websocket.bean.RgvInfo;
import com.yhy.wxzn.wms.WmsApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Auther: 97354
 * @Date: 2023/10/31 14:50
 * @Description: MapGenerator
 * @Version 1.0.0
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = WmsApplication.class)
@RunWith(SpringRunner.class)
public class MapGenerator {


    @Autowired
    WcsFourWayTaskServiceImpl wcsFourWayTaskService;



    @Test
    public void testTask(){
        WcsFourWayTask wcsFourWayTask = new WcsFourWayTask();
        wcsFourWayTask.setId(2);
        boolean b = wcsFourWayTaskService.updateRgvProgress(wcsFourWayTask, RgvProgress.RGV_TO_START);
        System.out.printf(String.valueOf(b));

    }



    @Test
    public void testTask1(){
        WcsFourWayTask wcsFourWayTask = new WcsFourWayTask();
        wcsFourWayTask.setTaskStatus(TaskStatus.CANCEL);
        wcsFourWayTaskService.save(wcsFourWayTask);
    }

    @Test
    public void testTask2(){
        List<WcsFourWayTask> list = wcsFourWayTaskService.list();
        System.out.println("");
    }

    @Test
    public void test4(){
        while (true){
            List<LiferInfo> liferInfos = LiferInfo.buildTestLiferInfos();
            System.out.println(JSON.toJSONString(liferInfos));
            WcsUtils.sleep(1000);
        }

    }

    @Test
    public void test5(){
        while (true){
            List<FloorInfo.RgvInfo> rgvInfos1 = FloorInfo.RgvInfo.buildTestRgvFloorInfos(1);
            List<RgvInfo> rgvInfos = RgvInfo.buildTestRgvInfos();
            System.out.println(JSON.toJSONString(rgvInfos));
            WcsUtils.sleep(1000);
        }

    }

    @Test
    public void test6(){
        while (true){
            NodeInfo nodeInfo = NodeInfo.buildNodInfo("1737378836272758785");
            System.out.println(JSON.toJSONString(nodeInfo));
            WcsUtils.sleep(1000);
        }

    }

    @Test
    public void test2(){
        while (true){
            FloorInfo floorInfo = FloorInfo.buildFloorInfo(1);
            System.out.println(JSON.toJSONString(floorInfo));
            WcsUtils.sleep(1000);
        }

    }

    @Test
    public void test3(){

        List<Node> nodeByFloor = SpringUtil.getBean(WcsMapInfoServiceImpl.class).getNodeByFloor(1);


//        HashMap<Node, Node> hashMap = new HashMap<>();
//
//        nodeByFloor.forEach(node -> hashMap.put(node,node));
//
//
//        Node node1 = new Node(1, 1, 1);
//        Node node2 = new Node(32, 91, 1);
//
//        boolean contains = nodeByFloor.contains(node1);
//        boolean contains2 = nodeByFloor.contains(node2);
//
//
//        Node node3 = hashMap.get(node1);
//        Node node4 = hashMap.get(node2);


        List<Route> routes = new PathPlaningHandlerImpl(nodeByFloor).calculatePath( new Node(15,14,1)
               ,  new Node(2,18,1)
                , false);

        Route route = routes.get(0);

        for (Node node : route.getPath()) {
            System.out.println(node);
        }

    }

    @Test
    public void test7(){
        WcsMapInfoServiceImpl bean = SpringUtil.getBean(WcsMapInfoServiceImpl.class);
        List<Node> nodeByFloor = bean.getNodeByFloor(1);


        Node node = new AvoidHandler(nodeByFloor).bfs1(bean.getNode(new Node(6, 2, 1)), true, false);
        System.out.println("避让点为"+node);

    }
    public static void main(String[] args) {
        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Node node = new Node();
            node.setX(i);
            node.setY(i);
            node.setZ(i);
            nodes.add(node);
        }

        HashMap<Node, Node> map = new HashMap<>();

        nodes.forEach(node -> map.put(node,node));

        for (int i = 0; i < 10; i++) {
            Node node = new Node();
            node.setX(i);
            node.setY(i);
            node.setZ(i);
            System.out.println(map.get(node));
            System.out.println(map.containsKey(node));
        }

    }


    @Test
    public void test8(){

        WcsDeviceInfoServiceImpl bean = SpringUtil.getBean(WcsDeviceInfoServiceImpl.class);

        bean.refreshRgvLocation(1,new Coord(1,1,1));

    }


    @Test
    public void test9(){

//        WcsTaskServiceImpl taskService = SpringUtil.getBean(WcsTaskServiceImpl.class);
//        WcsDeviceInfoServiceImpl deviceInfoService = SpringUtil.getBean(WcsDeviceInfoServiceImpl.class);
//        deviceInfoService.findByDeviceName("提升机1");
//
//        WcsTask task = taskService.list().get(0);
//
//        PlcTaskThread.executeTask(task);

    }


    @Test
    public void test10(){
        ToWmsImpl toWms = SpringUtil.getBean(ToWmsImpl.class);

//
//        TaskApply taskApply = new TaskApply(
//                DateUtil.format(new Date(), DateUtil.YMDHMS)
//                ,"PORT-A"
//                ,"barcode-001"
//                ,110
//                ,10.999
//        );
//        WmsResponses wmsResponses = toWms.exeTaskApply(taskApply);
//
//
//        System.out.println(JSON.toJSONString(wmsResponses));


        TaskStatusRtn rtn = new TaskStatusRtn();

        rtn.setReportTime(DateUtil.format(new Date(), DateUtil.YMDHMS));
        rtn.setReportInfo("测试数据");
        rtn.setTaskId("321");
        rtn.setTaskStatus(0);
        toWms.executionPhase(rtn);

    }

}
